//
//  JHSLogInDetailController.m
//  LogIn(登录)
//  archiver
//  Created by chenWei on 2017/3/22.
//  Copyright © 2017年 陈伟. All rights reserved.
//

#import "JHSLogInDetailController.h"
#import "Masonry.h"
#import "JHSUserStatus.h"
#import "JHSLeftBarViewController.h"
#import "JHSNetworkTool.h"
#import "JHSHomeViewController.h"
#import "JHSUserinfoModel.h"


@interface JHSLogInDetailController ()


@property (weak, nonatomic) IBOutlet UITextField *phoneNumField;

@property (weak, nonatomic) IBOutlet UITextField *pwdField;





@end

@implementation JHSLogInDetailController{
    //归档的数据
    NSDictionary *dic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pwdField.secureTextEntry=YES;
    self.view.backgroundColor = JHSMainColor;
    self.navigationController.navigationBar.translucent = NO;

    self.navigationController.navigationBarHidden = NO;
//    self.navigationController.navigationBar.alpha = 0.5;
    self.navigationItem.title = @"登录";
    

    
}


-(void)saveDataWithPath:(NSString *)pathStr{
    
    
    //就在这里归档最好
    [NSKeyedArchiver archiveRootObject:dic toFile:pathStr];
    
}

//MARK:2- 跳转到用户界面
- (IBAction)jump2Users:(UIButton *)sender {
//    NSLog(@"点击了登录");
    
    //先判断账号密码是否正确,如果正确 再请求数据
    //判断: 从本地取到账号密码是否和当前账号密码一致
    NSString *phoneStr = self.phoneNumField.text;
    NSString *pwdStr = self.pwdField.text;
    
    if ([phoneStr isEqualToString:@"13816138138"] && [pwdStr isEqualToString:@"123456"]) {
        
        
        //MARK:登入成功后请求用户的数据并跳转到首页
        /*1.快速登陆接口
         请求地址: http://iosapi.itcast.cn/doctor/carelinkQuickLogin.json.php
         请求参数:{"login_token":"985c96ce43bb5ef69de3e1ad8afaa83f"}
         登录成功后，用户id为：1000089
         */
        NSString *urlStr = @"http://iosapi.itcast.cn/doctor/carelinkQuickLogin.json.php";
        
        
        JHSNetworkTool  *manager = [JHSNetworkTool shareManager];
        NSDictionary *parameters = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
        
        //发起 post 请求
        [manager postWithURLStr:urlStr withParameters:parameters withSuccessBlock:^(id responseBody) {
            
            NSLog(@"%@",responseBody);
            if ([responseBody[@"msg"] isEqualToString:@"OK"]) {
                NSLog(@"登入成功");
                
                [SVProgressHUD showSuccessWithStatus:@"登录成功"];
//                // 延迟1秒后消失
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
                
                //把用户的数据保存到本地
                dic = responseBody[@"data"];
                JHSUserinfoModel *UserModel = [[JHSUserinfoModel alloc]init];
                UserModel.userDic = dic;
                //              [UserModel encodeWithCoder:]
                
                //初始化
                //   model = [JHSUserinfoModel new];
                //保存到沙盒
                //  model = UserModel;
                //保存到模型
                
                //              NSLog(@"===========%@",UserModel.true_name);
                NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userInfo.plist"];
                NSLog(@"%@",path);
                [self saveDataWithPath:path];
                //跳转到首页
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                NSLog(@"登入失败");
            }
            
        } withFailureBlock:^(NSError *error) {
            NSLog(@"%@",error);
            NSLog(@"数据请求失败");
            
        }];
        return;
    }else if([phoneStr isEqualToString:@"123456"] ){
        [SVProgressHUD showErrorWithStatus:@"密码错误"];
        // 延迟1秒后消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        NSLog(@"密码错误");
        return;
    }else{
        [SVProgressHUD showErrorWithStatus:@"账号错误"];
        // 延迟1秒后消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

        
        NSLog(@"账号错误");
        return;
    }
        
    
    
    
    
//    
//    [JHSUserStatus sharedUserAccount].isLogin=1;
//    
//    JHSLeftBarViewController *leftVC=[JHSLeftBarViewController new];
//    [leftVC loadView];
//    [self.navigationController popViewControllerAnimated:YES];
//    
    //
//    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"JHSUsersController" bundle:nil];
//    
//    
//    UIViewController *sbVC = [sb instantiateViewControllerWithIdentifier:@"storyBoard"];
//    
//    
//    [self.navigationController pushViewController:sbVC animated:YES];
    
    
}




//MARK:1-创建及添加子控件
- (void)setupUI
{
    
    
    
}



@end
